<?php

/*
	info: 校验验证码
*/

define('IS_MEMBER_CHECK', TRUE);
require '../common.inc.php';

global_CheckPost(
	array(
		array('code','str','',$weblang['api_codeError'],1),
));

if (!empty($webglobal['CPM']))
{
	$return['code'] = 1;
	$return['msg'] = reset($webglobal['CPM']);
	api_EchoReturn();
}

$webglobal['CPP']['id'] = api_GetTopActivityID();

$webglobal['codeinfo'] = $db->GetOne("SELECT * FROM promotion_user WHERE pid='{$webglobal['CPP']['id']}' AND uid='{$user['m_uid']}'");
if (!empty($webglobal['codeinfo']))
{
	$return['code'] = 0;
	api_EchoReturn();
}

$tempaddarray = array('pid'=>$webglobal['CPP']['id'], 'sid'=>0, 'uid'=>$user['m_uid'], 'peoplenum'=>0, 'code'=>$webglobal['CPP']['code'], 'codetime'=>$webserver['timestamp']);

if (defined('WEBLOCAL_TEST') && WEBLOCAL_TEST)
{
	if ($webglobal['CPP']['code']=='test1')
	{
		api_EchoReturn($weblang['api_codeOverdue']);
	}
	if ($webglobal['CPP']['code']=='test2')
	{
		api_EchoReturn($weblang['api_codeUsed']);
	}
	if ($webglobal['CPP']['code']=='test3')
	{
		$tempaddid = $db->Insert('promotion_user', $tempaddarray, TRUE);
		$return['code'] = 0;
		api_EchoReturn();
	}
	api_EchoReturn($weblang['api_codeError']);
}

$dbms = mssql_connect($webconfig['dbmshost'], $webconfig['dbmsuser'], $webconfig['dbmspass']);
if (!$dbms)
{
	$return['code'] = -1;
	$return['msg'] = $weblang['api_databaseError'];
	api_EchoReturn();
}
if (!mssql_select_db($webconfig['dbmsname'], $dbms))
{
	$return['code'] = -1;
	$return['msg'] = $weblang['api_databaseError'];
	api_EchoReturn();
}
$query = mssql_query("SELECT * FROM {$webconfig['dbmstable_voucher']} WHERE vou_number='{$webglobal['CPP']['code']}'", $dbms);
$webglobal['datainfo'] = mssql_fetch_array($query, MSSQL_ASSOC);
if (empty($webglobal['datainfo']))
{
	api_EchoReturn($weblang['api_codeError']);
}

$webglobal['datainfo']['exp_day_show'] = global_ConvTime($webglobal['datainfo']['exp_day']);
if ($webglobal['datainfo']['exp_day_show'] < $webserver['timestamp'])
{
	api_EchoReturn($weblang['api_codeOverdue']);
}
if (!empty($webglobal['datainfo']['actived_time']))
{
	api_EchoReturn($weblang['api_codeUsed']);
}

$tempcurtimestr = global_FormatTime($webserver['timestamp']);
$tempresult = mssql_query("UPDATE {$webconfig['dbmstable_voucher']} SET actived_time='{$tempcurtimestr}' WHERE vou_number='{$webglobal['CPP']['code']}'", $dbms);

$tempaddid = $db->Insert('promotion_user', $tempaddarray, TRUE);
$return['code'] = 0;
api_EchoReturn();
